package com.scalar.forecast.forecastSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.forecastSetup.forms.UserDefinedFieldsForm;
import com.scalar.forecast.forecastSetup.dao.ForecastUserFieldsDAO;
import com.scalar.forecast.commons.SessionKeys;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;

/**
 * User: Sujan Kumar
 * Date: Feb 9, 2005
 * Time: 6:17:10 PM
 */
public class ForecastUserFieldsNewCommand extends ActionCommand
{
     public ActionForward execute ( ActionMapping mapping,
                                            ActionForm form,
                                            HttpServletRequest request,
                                            HttpServletResponse response )
     {

        UserDefinedFieldsForm userDefinedFieldsForm = ( UserDefinedFieldsForm ) form;
        HashMap errors = new HashMap ();
        boolean success = false;
        if ( userDefinedFieldsForm == null )
        {
            userDefinedFieldsForm = new UserDefinedFieldsForm();
        }
        try
        {
           ForecastUserFieldsDAO.loadUserDefinedFields(null, userDefinedFieldsForm );
           success = true;
        }
        catch ( Exception e )
        {
            errors.put( ERROR_INTERNAL_SERVER_ERROR, ERROR_COULD_NOT_LOAD_FORECAST_USER_DEFINED_FIELDS_SCREEN );
            request.setAttribute ( SessionKeys.ERROR_MESSAGES, errors );
            e.printStackTrace();
        }
        ActionForward actionForward = null;
        if ( success )
        {
            actionForward = mapping.findForward( "success" );
        }
        else
        {
            actionForward = mapping.findForward ( "errorPage" );
        }
         return actionForward;
     }
}
